package com.unknown.wiki.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.unknown.common.util.LogUtils;
import com.unknown.communication.AndroidBus;
import com.unknown.communication.entity.Member;
import com.unknown.communication.entity.Message;
import com.unknown.wiki.manager.ServiceManager;

public class BaseActivity extends AppCompatActivity implements Member {
    private AndroidBus androidBus;
    private ServiceManager serviceManager;

    public ServiceManager getServiceManager() {
        return serviceManager;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
        registerToAndroidBus();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterToAndroidBus();
    }

    @Override
    public void onReceived(Message message) {
        LogUtils.d("received message %s", message);
    }

    public void sendMessage(Message message) {
        androidBus.send(message);
    }

    private void init() {
        androidBus = AndroidBus.getInstance();
        serviceManager = ServiceManager.getInstance();
    }

    private void registerToAndroidBus() {
        androidBus.register(this);
    }

    private void unregisterToAndroidBus() {
        androidBus.unregister(this);
    }
}
